﻿'use strict';

define([
    'backbone',
], function (Backbone) {
    return Backbone.Router.extend({
        currentView: null,
        currentRoute: null,
        mainViewContainer: null,
        views: {},
        mainTitleContainer: null,
        routesConfig: null,
        routesConfigByName: null,

        initialize: function (options) {
            options || (options = {});

            this.mainViewContainer = $(options.container);

            this.mainTitleContainer = $(options.titleContainer);

            this.routesConfigByName = {};

            //Get routes passed and build the correponding routes object.
            if (options.routesConfig)
            {
                this.routesConfig = options.routesConfig;

                for (var prop in this.routesConfig) {
                    var route = this.routesConfig[prop];

                    if ($.type(route) == 'string')
                    {
                        this.routesConfigByName[route] = prop;

                        this.route(prop, route, this._handleRoute);

                    }
                    else
                    {
                        this.routesConfigByName[route.name] = prop;

                        this.route(prop, route.name, this._handleRouteParams);
                    }
                }
            }


            // Prevent undesired navigation with anchors.
            $(document).on('click', 'a[data-bypass="true"]', function (evt) {
                evt.preventDefault();
            });

            return this;
        },

        execute: function (callback, args, name) {
            callback.apply(this, [this.routesConfigByName[name], name].concat(args));
        },
        _handleRoute: function () {
            this.switchView.apply(this, arguments);
        },
        _handleRouteParams: function (route, name) {
            var routeData = this.routesConfig[route];

            var routeArgs = routeData.parse((arguments.length > 2) ? $(arguments).slice(2) : null);

            this._handleRoute(route, name, routeArgs);
        },
        //route: function (route, name, callback) {
        //    this.switchView(route, name, callback);

        //    this.navigate(route);

        //    if (callback)
        //        callback();

        //    return this;
        //},
        switchView: function (route, name, routeArgs) {
            var that = this,
                viewModuleName = 'screens/' + name + '/' + name + '.view',
                modelModuleName = 'screens/' + name + '/' + name + '.model';

            require(
                [viewModuleName, modelModuleName], 
                function (viewCtr, modelCtr) {
                    var view;

                    //if (that.currentView != null) {
                    //    that.currentView.beforeClose
                    //}

                    if (that.views[route] == null) {
                        view = new viewCtr({
                            id: route,
                            model: new modelCtr(),
                        });

                        that.views[route] = view;

                        view.$el.hide();

                        that.mainViewContainer.append(view.el);

                        view.render();
                    }
                    else {
                        view = that.views[route];
                    }


                    if ((that.currentView != null) && (that.currentView.cid == view.cid)) {
                        that.currentView.show(routeArgs);
                    }
                    else {
                        if (that.currentView != null) {
                            that.currentView.hide();

                            if (that.currentView.cache != true) {
                                that.currentView.destroy();

                                delete that.views[that.currentRoute];
                            }
                        }

                        that.currentView = view;
                        that.currentRoute = route;



                        //Set website title with view title.
                        document.title = (view.title)
                                            ? view.title + ' - ' + App.config.title
                                            : App.config.title;

                        //Set main view title
                        that.mainTitleContainer.html(view.title);


                        if (that.currentView.beforeShow(routeArgs) == false)
                            that.currentView.on('ready', _.partial(that.currentView_ready, that.currentView, routeArgs), that);
                        else
                            that.currentView.show.apply(that.currentView, routeArgs);
                    }
                });

            return this;
        },


        currentView_ready: function (view, routeArgs) {
            if (this.currentView.name == view.name)
                view.show(routeArgs);
        },
    });
});